package cn.com.liuxiaoluxx.cloud.user.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.com.liuxiaoluxx.cloud.facade.pojo.AuthUser;
import cn.com.liuxiaoluxx.cloud.facade.pojo.Role;
import cn.com.liuxiaoluxx.cloud.facade.pojo.User;
import cn.com.liuxiaoluxx.cloud.facade.pojo.UserRole;
import cn.com.liuxiaoluxx.cloud.facade.request.LoginRequest;
import cn.com.liuxiaoluxx.cloud.facade.service.user.RoleService;
import cn.com.liuxiaoluxx.cloud.facade.service.user.UserRoleService;
import cn.com.liuxiaoluxx.cloud.facade.service.user.UserService;
import cn.com.liuxiaoluxx.cloud.facade.utils.AssertUtil;
import cn.com.liuxiaoluxx.cloud.user.mapper.UserMapper;

/**
 * The type IntelliJ IDEA.
 * <p>
 *
 * @author liuxiaolu
 * @date 2020/9/17
 */
@Component
@Service(version = "1.0.0", interfaceName = "cn.com.liuxiaoluxx.cloud.facade.service.user.UserService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public AuthUser login(LoginRequest request) {
        return authUser(request.getUsername(), request.getPassword());
    }

    @Override
    public List<User> selectAllUser() {
        return baseMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public User selectByUserName(String username) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("name", username));
        AssertUtil.assertNotNull(user, "指定用户[" + username + "]不存在");

        return user;
    }

    @Override
    public AuthUser authUser(String username, String password) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("name", username).eq("password", password));

        AuthUser authUser = new AuthUser();
        BeanUtils.copyProperties(user, authUser);
        return authUser;
    }

    @Override
    public List<UserRole> findSysRoleListByUsername(String username) {
        User user = selectByUserName(username);

        List<UserRole> userRoles = userRoleService.getBaseMapper()
                                                  .selectList(new QueryWrapper<UserRole>().eq("user_id", user.getId()));

        if (CollectionUtils.isEmpty(userRoles)) {
            return userRoles;
        }

        List<Long> userRoleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        List<Role> roles = roleService.getBaseMapper().selectList(new QueryWrapper<Role>().in("id", userRoleIds));
        Map<Long, String> roleMap = roles.stream().collect(Collectors.toMap(Role::getId, Role::getRoleCode));

        userRoles.forEach(userRole -> userRole.setRoleCode(roleMap.get(userRole.getRoleId())));
        return userRoles;
    }
}
